package com.lanou.musicproject.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.Constant;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/20.
 * 音乐馆右侧页面
 */
public class RightFragment extends Fragment {

    private ViewPager viewPager;
    private ArrayList<Fragment> data;
    private MyAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rightView = inflater.inflate(R.layout.fragment_right, null);
        initView(rightView);
        return rightView;
    }

    public ViewPager getViewPager() {
        return viewPager;
    }

    private void initView(View v) {
        viewPager = (ViewPager) v.findViewById(R.id.right_fragment_viewpager);

        data = new ArrayList<>();
        data.add(new RightAllFragment(Constant.TOP_100));
        data.add(new RightAllFragment(Constant.CHINESE_MAN));
        data.add(new RightAllFragment(Constant.CHINESE_WOMAN));
        data.add(new RightAllFragment(Constant.CHINESE_GROUP));
        data.add(new RightAllFragment(Constant.WEST_MAN));
        data.add(new RightAllFragment(Constant.WEST_WOMAN));
        data.add(new RightAllFragment(Constant.WEST_GROUP));
        data.add(new RightAllFragment(Constant.KOREA_MAN));
        data.add(new RightAllFragment(Constant.KOREA_WOMAN));
        data.add(new RightAllFragment(Constant.KOREA_GROUP));
        adapter = new MyAdapter(getActivity().getSupportFragmentManager(), data);
        viewPager.setAdapter(adapter);
    }

    /**
     * 内部类
     */
    private class MyAdapter extends FragmentPagerAdapter {

        private ArrayList<Fragment> datas;

        public MyAdapter(FragmentManager fm, ArrayList<Fragment> data) {
            super(fm);
            this.datas = data;
        }

        @Override
        public Fragment getItem(int position) {
            return datas != null && datas.size() > 0 ? datas.get(position) : null;
        }

        @Override
        public int getCount() {
            return datas != null && datas.size() > 0 ? datas.size() : 0;
        }
    }
}
